package com.test.clyq.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.clyq.entity.Comment;
import com.test.clyq.entity.Users;
import com.test.clyq.service.CommentService;
import com.test.json.JsonUtil;
import com.test.util.FormatExceptionStack;
import com.test.util.ObjectUtils;
import com.test.util.Pagination;
import com.test.util.SessionUtil;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * CommentAction. <br>
 * 评论管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class CommentAction extends BaseAction {

	/**
	 * 删除评论
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward deleteComment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("删除评论start");
		CommentService commentService = (CommentService) getBean("commentService");
		String commentId = getParameter(request, "commentId");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (StringUtils.isNumeric(commentId) && !ObjectUtils.isNull(user)) {
				commentService.deleteComment(Long.valueOf(commentId), user);
				print(response, true);
			} else {
				print(response, false);
			}
			this.log.debug("删除评论成功");
			this.log.info("删除评论end");
		}
		catch (Exception e) {
			this.log.error("删除评论:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}

	/**
	 * 获得一个评论
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getComment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得一个评论表start");
		CommentService commentService = (CommentService) getBean("commentService");
		try {
			String commentId = getParameter(request, "commentId");
			Comment comment = null;
			if (StringUtils.isNumeric(commentId)) {
				Long id = Long.valueOf(commentId);
				comment = commentService.getOneComment(id);
				if (ObjectUtils.isNull(comment)) {
					print(response, false);
					return null;
				}
				this.log.debug("获得一个下载文件成功");
				this.log.info("获得一个下载文件end");
				print(response, "{results:[{%s}]}", null, null, comment.toJSON());
			} else {
				print(response, false);
			}
			this.log.debug("获得一个评论成功");
			this.log.info("获得一个评论end");
		}
		catch (Exception e) {
			this.log.error("获得一个评论:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 获得评论列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getComments(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得评论列表start");
		CommentService commentService = (CommentService) getBean("commentService");
		try {
			String start = getParameter(request, "start");
			String limit = getParameter(request, "limit");
			String pid = getParameter(request, "pid");// 当前的文章/图片id
			String type = getParameter(request, "type");
			if (StringUtils.isNumeric(start) && StringUtils.isNumeric(limit) && StringUtils.isNumeric(pid) && StringUtils.isNumeric(type)) {
				int currpageNo = 1;
				int pageCount = Integer.parseInt(limit);
				if (Integer.parseInt(start) / pageCount != 0) {
					currpageNo = Integer.parseInt(start) / pageCount + 1;// 当前页码

				}
				Pagination pagination = new Pagination(currpageNo, pageCount);// 设置分页参数（页码和每页显示记录条数）
				int totalCount = 0;
				List<Comment> list = commentService.getCommentByPage(pagination, Long.valueOf(pid), Integer.valueOf(type));
				totalCount = pagination.getTotalRows();
				String jsonStr = JsonUtil.getJsonStr(list, new String[] { "commentId", "commentContent", "mobile", "commentTime", "commentStatus" },
				                                     totalCount);
				this.log.debug("获得评论列表成功");
				this.log.info("获得评论列表end");
				print(response, jsonStr, null, null);
			}
		}
		catch (Exception e) {
			this.log.error("获得评论:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 审核一个评论
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updatedownloadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("修改一个评论表start");
		CommentService commentService = (CommentService) getBean("commentService");
		String commentId = getParameter(request, "commentId");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (StringUtils.isNumeric(commentId) && !ObjectUtils.isNull(user)) {
				Comment comment = commentService.getOneComment(Long.valueOf(commentId));
				if (ObjectUtils.isNull(comment)) {
					print(response, false);
				} else {
					if (comment.getCommentStatus() == 0) {
						comment.setCommentStatus(1);
					} else {
						comment.setCommentStatus(0);
					}
					commentService.saveOrUpdateComment(comment, user);
					print(response, true);
				}
			} else {
				print(response, false);
			}
			this.log.debug("修改一个评论成功");
			this.log.info("修改一个评论end");
		}
		catch (Exception e) {
			this.log.error("修改一个评论:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

}
